package com.pronosticnews.client.admin.activity;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.event.shared.EventBus;
import com.pronosticnews.client.admin.ClientFactory;
import com.pronosticnews.client.admin.view.editSport.EditSportView;
import com.pronosticnews.client.admin.view.editSport.EditSportView.Presenter;
import com.pronosticnews.client.event.DeletedSportEvent;
import com.pronosticnews.client.event.DeletedSportEventHandler;
import com.pronosticnews.client.event.InformationMessageEvent;
import com.pronosticnews.client.event.NouveauSportEvent;
import com.pronosticnews.client.event.SelectedSportEvent;
import com.pronosticnews.client.event.SelectedSportEventHandler;
import com.pronosticnews.client.services.SportService;
import com.pronosticnews.client.services.SportServiceAsync;
import com.pronosticnews.client.util.InformationMessage;
import com.pronosticnews.client.util.InformationMessage.TYPE_MESSAGE;
import com.pronosticnews.shared.model.SportDto;

public class EditSportActivity extends AbstractActivity implements Presenter {

	private EditSportView view;

	private PlaceController placeController;

	private SportServiceAsync sportService;

	private AsyncCallback<SportDto> saveCallBack;

	private EventBus eventBus;

	private SportDto existingSport;

	public EditSportActivity(ClientFactory clientFactory) {
		sportService = GWT.create(SportService.class);
		placeController = clientFactory.getPlaceController();
		view = clientFactory.getEditSportView();
		view.setPresenter(this);
		eventBus = clientFactory.getEventBus();

		saveCallBack = new AsyncCallback<SportDto>() {

			@Override
			public void onSuccess(SportDto result) {
				InformationMessage messageSuccess = new InformationMessage();
				messageSuccess.setType(TYPE_MESSAGE.SUCCESS);
				List<String> messages = new ArrayList<String>();
				messages.add("Le sport a été créé/modifié avec succès !");
				messageSuccess.setText(messages);
				view.submitComplete();
				eventBus.fireEvent(new NouveauSportEvent());
				eventBus.fireEvent(new InformationMessageEvent(messageSuccess));
			}

			@Override
			public void onFailure(Throwable caught) {
				InformationMessage erreur = new InformationMessage();
				List<String> messages = new ArrayList<String>();
				messages.add(caught.getMessage());
				erreur.setText(messages);
				erreur.setType(TYPE_MESSAGE.ERROR);
				eventBus.fireEvent(new InformationMessageEvent(erreur));
			}
		};

		eventBus.addHandler(SelectedSportEvent.TYPE,
				new SelectedSportEventHandler() {

					@Override
					public void onSelect(SelectedSportEvent event) {
						existingSport = event.getSportSelected();
						view.setModeModification(true);
						view.setSport(existingSport);
					}
				});

		eventBus.addHandler(DeletedSportEvent.TYPE,
				new DeletedSportEventHandler() {

					@Override
					public void onDelete(DeletedSportEvent event) {
						existingSport = null;
						view.setModeModification(false);
					}
				});

		view.setModeModification(false);

	}

	@Override
	public void start(AcceptsOneWidget panel,
			com.google.gwt.event.shared.EventBus eventBus) {

		panel.setWidget(view.asWidget());
	}

	@Override
	public void submit(SportDto editSport) {
		if (existingSport != null) {
			editSport.setId(existingSport.getId());
			sportService.modifier(editSport, saveCallBack);
		} else {
			sportService.sauvegarder(editSport, saveCallBack);
		}
	}

	@Override
	public void annuleModification() {
		existingSport = null;
	}

}
